// @ts-nocheck
import React, { useEffect, useState } from "react";
import { NavBar } from "antd-mobile";
import { useNavigate } from "react-router-dom";
import "./index.less";
import { getCurrentUserInfo } from "@/utils/authority";
import { getHistoryReportList } from "../../service";


type serviceListType = {
	id: number;
	name: string;
	state: string;
};
const HistoryReport: React.FC<any> = () => {
	const navigate = useNavigate();
	const userId = getCurrentUserInfo()?.data?.id;

	const [list, setList] = useState<serviceListType[]>([]);
	const getListData = async () => {
		const params = {
			assignee: userId,
			current: 1,
			size: 10000,
		};
		try {
			const { data } = await getHistoryReportList(params);
			setList(data?.records);

		} catch {}
	};
	useEffect(() => {
		getListData();
	}, []);
	function safeParseJSON(jsonString, defaultValue = {}) {
		try {
			return JSON.parse(jsonString);
		} catch (error) {
			console.error("Error parsing JSON:", error);
			return defaultValue;
		}
	}


	return (
		<div className="historyReport-contarnier">
			<div className="header">
				<div>
					<NavBar onBack={() => navigate("/user/trends")}>
						<div className="commontitle">上报历史</div>
					</NavBar>
				</div>
			</div>
			<div className="historyReport-box">
				{list.map((item) => (
					<div
						className="historyReport-box-card"
						key={item.id}
						onClick={() => navigate("/user/trends/historyReportDetails", { state: item })}>
						<div className="historyReport-box-card-title">{safeParseJSON(item?.formCommitData)?.questiontitle || ""}</div>
						<div className="historyReport-box-card-type">
							<span>{safeParseJSON(item?.formCommitData)?.eventType || ""}</span>
						</div>
						<div className="historyReport-box-card-time">{safeParseJSON(item?.formCommitData)?.createTime || ""}</div>
						<div className={item?.processInstanceStatus == "COMPLETED" ? "ycl":"ysl"}>{item?.processInstanceStatus == "COMPLETED" ? "已完结":"已处理"}</div>
					</div>
				))}
			</div>
		</div>
	);
};

export default HistoryReport;
